package com.topscomm.mis.cbb.pm.service.abs;

import com.google.j2objc.annotations.J2ObjCIncompatible;
import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.exception.ServiceException;
import com.topscomm.mis.cbb.pm.api.IReplayService;
import com.topscomm.mis.cbb.pm.base.pojo.CbbPmReplayEntity;
import com.topscomm.mis.cbb.pm.base.service.ICbbPmReplayService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.Query;
import com.topscomm.tap.common.DatacodeCache;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javafx.print.Collation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.transaction.annotation.Transactional;
import sun.text.resources.uk.CollationData_uk;

/**
 * @author: caozhiyuan
 * @date: 2023-07-24
 * @descripyion: 复盘会
 * @modify:
 */
@ConditionalOnMissingBean(IReplayService.class)
public abstract class AbstractReplayService implements IReplayService {
    @Autowired
    protected ICbbPmReplayService cbbPmReplayService;

    @Autowired
    protected DatacodeCache datacodeCache;


    /**
     * description 插入数据
     * @author wxm
     * @date 2023-06-25 18:08:22
     * @param paramMap
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @modify
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> insert(Map<String, Object> paramMap) {
        beforeInsert(paramMap);
        CbbPmReplayEntity pmReplayEntity = new CbbPmReplayEntity();
        pmReplayEntity.convertFromMap(paramMap);
        pmReplayEntity = cbbPmReplayService.insert(pmReplayEntity);
        Map<String, Object> dataMap = pmReplayEntity.convertToMap();
        afterInsert(dataMap);
        return dataMap;
    }

    /**
     * description 批量插入
     * @author wxm
     * @date 2023-06-27 22:22:25
     * @param paramMap
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @modify
     */
    @Override
    public List<Map<String, Object>> insertBatch(List<Map<String, Object>> paramMap) {
        List<BasicEntity> basicEntityList = new ArrayList<>();
        paramMap.forEach(p->{
            CbbPmReplayEntity pmReplayEntity = new CbbPmReplayEntity();
            pmReplayEntity.convertFromMap(p);
            basicEntityList.add(pmReplayEntity);
        });
        beforeInsertBatch(basicEntityList);
        cbbPmReplayService.insertBatch(basicEntityList);
        List<Map<String,Object>> resultMapList = new ArrayList<>();
        for (BasicEntity basicEntity : basicEntityList) {
            resultMapList.add(basicEntity.convertToMap());
        }
        return resultMapList;
    }


    protected void beforeInsertBatch(List<BasicEntity> basicEntityList) {
    }

    protected void beforeInsert(Map<String, Object> paramMap) throws ServiceException {
    }

    protected void afterInsert(Map<String, Object> paramMap) throws ServiceException {
    }

    /**
     * description 获取列表数据
     * @author wxm
     * @date 2023-06-25 18:08:11
     * @param paraMap
     * @param sqlWhere
     * @return com.topscomm.pub.vo.Page
     * @modify
     */
    @Override
    public Page list(Map<String, Object> paraMap, String sqlWhere) {
        Query query = new Query(paraMap);
        int totalCount = cbbPmReplayService.getCount(sqlWhere);
        List<Map<String, Object>> tempList = null;
        if (query.getPageSize() > 0) {
            tempList = cbbPmReplayService.queryMapForPage(sqlWhere, query.getCurrentPage(), query.getPageSize(),
                query.getSidx(), query.getSord());
        } else {
            tempList = cbbPmReplayService.queryMapByWhere(sqlWhere);
        }
        setDispFields(tempList);
        return new Page(tempList, totalCount, query.getPageSize(), query.getCurrentPage());
    }


    protected void setDispFields(List<Map<String, Object>> mapRowList) {
    }

    /**
     * description 更新
     * @author wxm
     * @date 2023-06-25 18:16:33
     * @param paramMap
     * @return void
     * @modify
     */
    @Override
    public void update(Map<String, Object> paramMap) {
        cbbPmReplayService.update(paramMap);
    }

    /**
     * description 删除
     * @author wxm
     * @date 2023-06-25 18:20:33
     * @param paraMap
     * @return void
     * @modify
     */
    @Override
    public void delete(Map<String, Object> paraMap) {
        String ids = ConvertUtil.convertToString(paraMap.get("ids"));
        cbbPmReplayService.deleteByWhere(" id in (" + ids + ")");
    }

    /**
     * description 加载详情页
     * @author wxm
     * @date 2023-06-25 18:45:12
     * @param paraMap
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @modify
     */
    @Override
    public Map<String, Object> load(Map<String, Object> paraMap) {
        return cbbPmReplayService.queryMapById(paraMap.get("id").toString());
    }

    @Override
    public Map<String, Object> loadByProjectUUcode(Map<String, Object> paraMap) {
        StringBufferProxy whereSql = new StringBufferProxy();
        whereSql.appendSingle(" projectuucode = '{0}' ",ConvertUtil.convertToString(paraMap.get("projectUUCode")));
        List<Map<String, Object>> resultMapList = cbbPmReplayService.queryMapByWhere(whereSql.toString());
        if (!CollectionUtils.isEmpty(resultMapList)) {
            return resultMapList.get(0);
        } else {
            return null;
        }
    }
}
